﻿Imports System.DirectoryServices

Public Class FormConnect

    Private Sub ButtonTester_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTester.Click
        Me.Cursor = Cursors.WaitCursor
        Dim oContext As System.DirectoryServices.ActiveDirectory.DirectoryContext
        Dim oDomain As System.DirectoryServices.ActiveDirectory.Domain
        If Me.CheckBoxUser.Checked Then
            oContext = New System.DirectoryServices.ActiveDirectory.DirectoryContext(ActiveDirectory.DirectoryContextType.Domain, Me.ComboBoxDomaine.Text, Me.TextBoxUserName.Text, Me.TextBoxPassword.Text)
        Else
            oContext = New System.DirectoryServices.ActiveDirectory.DirectoryContext(ActiveDirectory.DirectoryContextType.Domain, Me.ComboBoxDomaine.Text)
        End If
        oDomain = System.DirectoryServices.ActiveDirectory.Domain.GetDomain(oContext)
        Me.DirectorySearcher1.SearchRoot = oDomain.GetDirectoryEntry()
        Me.DirectorySearcher1.Filter = "(objectCategory=computer)"
        Me.DirectorySearcher1.PageSize = 1000
        Dim oResult As SearchResult
        Try
            oResult = Me.DirectorySearcher1.FindOne()
            Me.Cursor = Cursors.Default
            MsgBox("Connexion réussie!", MsgBoxStyle.Information)
        Catch Ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox("Erreur de connexion: " & Ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub CheckBoxUser_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxUser.CheckedChanged
        Me.TextBoxUserName.Enabled = Me.CheckBoxUser.Checked
        Me.TextBoxPassword.Enabled = Me.CheckBoxUser.Checked
    End Sub

    Private Sub FormConnect_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        My.Application.DoEvents()
        '-- DOMAINE --
        'Récupérer les domaines du projet courant
        Me.ComboBoxDomaine.Items.Clear()
        For Each sDomaine As String In Form1.ProjetCourant.ColDomainesLDAP
            Me.ComboBoxDomaine.Items.Add(sDomaine)
        Next
        'Domaines de la forêt courante
        Dim oForest As System.DirectoryServices.ActiveDirectory.Forest
        Try
            oForest = System.DirectoryServices.ActiveDirectory.Forest.GetCurrentForest()
            For Each oDomain As System.DirectoryServices.ActiveDirectory.Domain In oForest.Domains
                If Not Me.ComboBoxDomaine.Items.Contains(oDomain.Name) Then Me.ComboBoxDomaine.Items.Add(oDomain.Name)
            Next
        Catch Ex As Exception
            'On est en workgroup
        End Try
        '-- FILTRE --
        'Récupérer les requêtes LDAP du projet courant
        Me.ComboBoxFiltre.Items.Clear()
        If Me.ComboBoxFiltre.Enabled Then
            For Each sFiltre As String In Form1.ProjetCourant.ColFiltreLDAP
                Me.ComboBoxFiltre.Items.Add(sFiltre)
            Next
            'On se rabat sur les requêtes par défaut s'il n'y en a pas...
            If Me.ComboBoxFiltre.Items.Count = 0 Then
                For Each sFiltre As String In My.Settings.DefautFiltreLDAP
                    Me.ComboBoxFiltre.Items.Add(sFiltre)
                Next
            End If
            'Dernière active
            Me.ComboBoxFiltre.Text = Form1.ProjetCourant.FiltreLDAP
        Else
            Me.ComboBoxFiltre.Text = "(objectCategory=computer)"
        End If
        If (Me.ComboBoxFiltre.Text = "") And (Me.ComboBoxFiltre.Items.Count > 0) Then Me.ComboBoxFiltre.Text = Me.ComboBoxFiltre.Items(0)
        '-- PROPRIÉTÉS
        Me.ComboBoxProp.Items.Clear()
        For Each sProp As String In Form1.ProjetCourant.ColProprietesLDAP
            Me.ComboBoxProp.Items.Add(sProp)
        Next
        'On se rabat sur les requêtes par défaut s'il n'y en a pas...
        If Me.ComboBoxProp.Items.Count = 0 Then
            Me.ComboBoxProp.Items.Add("Name,DistinguishedName")
        End If
        'Dernière active
        Me.ComboBoxProp.Text = Form1.ProjetCourant.ProprietesLDAP
        If (Me.ComboBoxProp.Text = "") And (Me.ComboBoxProp.Items.Count > 0) Then Me.ComboBoxProp.Text = Me.ComboBoxProp.Items(0)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        FormBrowser.WebBrowser1.DocumentText = My.Resources.LDAPHelp
        FormBrowser.Show()
    End Sub

    Private Sub ButtonOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOk.Click
        'On garde les domaines
        If Not Me.ComboBoxDomaine.Items.Contains(Me.ComboBoxDomaine.Text) Then Me.ComboBoxDomaine.Items.Add(Me.ComboBoxDomaine.Text)
        Form1.ProjetCourant.ColDomainesLDAP.Clear()
        For Each sDomaine As String In Me.ComboBoxDomaine.Items
            Form1.ProjetCourant.ColDomainesLDAP.Add(sDomaine)
        Next
        Form1.ProjetCourant.DomaineLDAP = Me.ComboBoxDomaine.Text
        'On garde les requetes LDAP
        If Not Me.ComboBoxFiltre.Items.Contains(Me.ComboBoxFiltre.Text) Then Me.ComboBoxFiltre.Items.Add(Me.ComboBoxFiltre.Text)
        Form1.ProjetCourant.ColFiltreLDAP.Clear()
        For Each sFiltre As String In Me.ComboBoxFiltre.Items
            Form1.ProjetCourant.ColFiltreLDAP.Add(sFiltre)
        Next
        Form1.ProjetCourant.FiltreLDAP = Me.ComboBoxFiltre.Text
        'Et les propriétés
        If Not Me.ComboBoxProp.Items.Contains(Me.ComboBoxProp.Text) Then Me.ComboBoxProp.Items.Add(Me.ComboBoxProp.Text)
        Form1.ProjetCourant.ColProprietesLDAP.Clear()
        For Each sProp As String In Me.ComboBoxProp.Items
            Form1.ProjetCourant.ColProprietesLDAP.Add(sProp)
        Next
        Form1.ProjetCourant.ProprietesLDAP = Me.ComboBoxProp.Text
    End Sub

    Private Sub ButtonDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDel.Click
        If Me.ComboBoxFiltre.Items.Contains(Me.ComboBoxFiltre.Text) Then
            Me.ComboBoxFiltre.Items.Remove(Me.ComboBoxFiltre.Text)
            Me.ComboBoxFiltre.Text = ""
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.ComboBoxDomaine.Items.Contains(Me.ComboBoxDomaine.Text) Then
            Me.ComboBoxDomaine.Items.Remove(Me.ComboBoxDomaine.Text)
            Me.ComboBoxDomaine.Text = ""
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Me.ComboBoxProp.Items.Contains(Me.ComboBoxProp.Text) Then
            Me.ComboBoxProp.Items.Remove(Me.ComboBoxProp.Text)
            Me.ComboBoxProp.Text = ""
        End If
    End Sub

End Class